package com.ruoyi.xiaoshou.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.xiaoshou.domain.SaleBack;
import com.ruoyi.xiaoshou.domain.SaleBackDetail;
import com.ruoyi.xiaoshou.mapper.SaleBackDetailMapper;
import com.ruoyi.xiaoshou.mapper.SaleBackMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xiaoshou.mapper.SaleBackRequestDetailMapper;
import com.ruoyi.xiaoshou.domain.SaleBackRequestDetail;
import com.ruoyi.xiaoshou.service.ISaleBackRequestDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售退货申请详情Service业务层处理
 *
 * @author lmj
 * @date 2024-10-25
 */
@Service
@Slf4j
@Transactional
public class SaleBackRequestDetailServiceImpl implements ISaleBackRequestDetailService {
    @Autowired
    private SaleBackRequestDetailMapper saleBackRequestDetailMapper;
    @Autowired
    private SaleBackMapper saleBackMapper;
    @Autowired
    private SaleBackDetailMapper saleBackDetailMapper;

    /**
     * 查询销售退货申请详情
     *
     * @param id 销售退货申请详情主键
     * @return 销售退货申请详情
     */
    @Override
    public SaleBackRequestDetail selectSaleBackRequestDetailById(Integer id) {
        return saleBackRequestDetailMapper.selectSaleBackRequestDetailById(id);
    }

    /**
     * 查询销售退货申请详情列表
     *
     * @param saleBackRequestDetail 销售退货申请详情
     * @return 销售退货申请详情
     */
    @Override
    public List<SaleBackRequestDetail> selectSaleBackRequestDetailList(SaleBackRequestDetail saleBackRequestDetail) {
        return saleBackRequestDetailMapper.selectSaleBackRequestDetailList(saleBackRequestDetail);
    }

    /**
     * 查询可以应用的退货申请详情
     *
     * @param saleBackRequestDetail 参数
     * @return 退货列表
     */
    @Override
    public List<SaleBackRequestDetail> useSaleBackRequestDetailList(SaleBackRequestDetail saleBackRequestDetail) {
        log.info("SaleBackRequestDetailServiceImpl useSaleBackRequestDetailList params:{}", saleBackRequestDetail);
        //先查询申请详情，获取详情列表，再根据编号查询退货订单，由订单查询对应的详情
        List<SaleBackRequestDetail> saleBackRequestDetails = saleBackRequestDetailMapper.selectSaleBackRequestDetailList(saleBackRequestDetail);
        SaleBack saleBack = new SaleBack();
        saleBack.setSaleBackRequestCode(saleBackRequestDetail.getSaleBackRequestCode());
        List<SaleBack> saleBacks = saleBackMapper.selectSaleBackList(saleBack);
        //如果申请单有对应绑定的退货订单就继续
        if (saleBacks.isEmpty()) {
            return saleBackRequestDetails;
        }
        // 获取 SaleBackCode 数组
        // 将 List 转换为数组
        String[] codeArray = saleBacks.stream().map(SaleBack::getSaleBackCode).toArray(String[]::new);
        List<SaleBackDetail> saleBackDetails = saleBackDetailMapper.getSaleBackDetailByCodes(codeArray);
        //如果有详情就继续
        if (saleBackDetails.isEmpty()) {
            return saleBackRequestDetails;
        }
        //最终结果
        List<SaleBackRequestDetail> saleBackRequestDetails1 = new ArrayList<>();
        for (SaleBackRequestDetail saleBackRequestDetail1 : saleBackRequestDetails) {
            for (SaleBackDetail saleBackDetail : saleBackDetails) {
                if (saleBackDetail.getProCode().equals(saleBackRequestDetail1.getProCode())) {
                    int proNum = saleBackRequestDetail1.getProNum() - saleBackDetail.getProNum();
                    saleBackRequestDetail1.setProNum(Math.max(proNum, 0));
                }
            }
            if (saleBackRequestDetail1.getProNum() > 0) {
                saleBackRequestDetails1.add(saleBackRequestDetail1);
            }
        }
        return saleBackRequestDetails1;
    }

    @Override
    public List<SaleBackRequestDetail> getSaleBackRequestDetailByCodes(String[] codeArray) {
        log.info("SaleBackRequestDetailServiceImpl getSaleBackDetailByCodes params:{}", (Object) codeArray);
        List<SaleBackRequestDetail> saleBackRequestDetails = saleBackRequestDetailMapper.getSaleBackRequestDetailByCodes(codeArray);
        return saleBackRequestDetails;
    }

    /**
     * 新增销售退货申请详情
     *
     * @param saleBackRequestDetail 销售退货申请详情
     * @return 结果
     */
    @Override
    public int insertSaleBackRequestDetail(SaleBackRequestDetail saleBackRequestDetail) {
        return saleBackRequestDetailMapper.insertSaleBackRequestDetail(saleBackRequestDetail);
    }

    @Override
    public AjaxResult insertSaleBackRequestDetails(List<SaleBackRequestDetail> saleBackRequestDetails) {
        log.info("SaleBackRequestDetailServiceImpl insertSaleBackRequestDetails params:{}", saleBackRequestDetails);
        for (SaleBackRequestDetail saleBackRequestDetail : saleBackRequestDetails) {
            if (insertSaleBackRequestDetail(saleBackRequestDetail) <= 0) {
                AjaxResult.error("退货详情添加失败！");
                throw new RuntimeException("退货详情添加失败！");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改销售退货申请详情
     *
     * @param saleBackRequestDetail 销售退货申请详情
     * @return 结果
     */
    @Override
    public int updateSaleBackRequestDetail(SaleBackRequestDetail saleBackRequestDetail) {
        return saleBackRequestDetailMapper.updateSaleBackRequestDetail(saleBackRequestDetail);
    }

    @Override
    public AjaxResult updateSaleBackRequestDetails(List<SaleBackRequestDetail> saleBackRequestDetails) {
        log.info("SaleBackRequestDetailServiceImpl updateSaleBackRequestDetails params:{}", saleBackRequestDetails);
        //删除saleBackRequestCode等同的详情，再添加
        String saleBackRequestCode = saleBackRequestDetails.get(0).getSaleBackRequestCode();
        boolean flag = saleBackRequestDetailMapper.deleteSaleBackRequestDetailBySaleBackRequest(saleBackRequestCode) > 0;
        if (!flag) {
            log.error("deleteSaleBackRequestDetailBySaleBackRequest fail");
            throw new RuntimeException("删除详情失败");
        }
        for (SaleBackRequestDetail saleBackRequestDetail : saleBackRequestDetails) {
            if (saleBackRequestDetailMapper.insertSaleBackRequestDetail(saleBackRequestDetail) <= 0) {
                log.error("insertSaleBackRequestDetail fail");
                throw new RuntimeException("添加详情失败");
            }
        }
        return AjaxResult.success();
    }

    /**
     * 批量删除销售退货申请详情
     *
     * @param ids 需要删除的销售退货申请详情主键
     * @return 结果
     */
    @Override
    public int deleteSaleBackRequestDetailByIds(Integer[] ids) {
        return saleBackRequestDetailMapper.deleteSaleBackRequestDetailByIds(ids);
    }

    /**
     * 删除销售退货申请详情信息
     *
     * @param id 销售退货申请详情主键
     * @return 结果
     */
    @Override
    public int deleteSaleBackRequestDetailById(Integer id) {
        return saleBackRequestDetailMapper.deleteSaleBackRequestDetailById(id);
    }
}
